<?

/** mb_trim($string, $charlist='\\\\s', $ltrim=true, $rtrim=true)
 *
 * Trim characters from either (or both) ends of a string in a way that is 
 * multibyte-friendly. 
 * 
 * Mostly, this behaves exactly like trim() would: for example supplying 'abc' as 
 * the charlist will trim all 'a', 'b' and 'c' chars from the string, with, of 
 * course, the added bonus that you can put unicode characters in the charlist. 
 * 
 * We are using a PCRE character-class to do the trimming in a unicode-aware 
 * way, so we must escape ^, \, - and ] which have special meanings here. 
 * As you would expect, a single \ in the charlist is interpretted as 
 * "trim backslashes" (and duly escaped into a double-\ ). Under most circumstances 
 * you can ignore this detail. 
 * 
 * As a bonus, however, we also allow PCRE special character-classes (such as '\s') 
 * because they can be extremely useful when dealing with UCS. '\pZ', for example, 
 * matches every 'separator' character defined in Unicode, including non-breaking 
 * and zero-width spaces. 
 * 
 * It doesn't make sense to have two or more of the same character in a character 
 * class, therefore we interpret a double \ in the character list to mean a 
 * single \ in the regex, allowing you to safely mix normal characters with PCRE 
 * special classes. 
 * 
 * *Be careful* when using this bonus feature, as PHP also interprets backslashes 
 * as escape characters before they are even seen by the regex. Therefore, to 
 * specify '\\s' in the regex (which will be converted to the special character 
 * class '\s' for trimming), you will usually have to put *4* backslashes in the 
 * PHP code - as you can see from the default value of $charlist. 
 * 
 * @param string 
 * @param charlist list of characters to remove from the ends of this string. 
 * @param boolean trim the left? 
 * @param boolean trim the right? 
 * @return String 
 */ 
    
function mb_trim($string, $charlist='\\\\s', $ltrim=true, $rtrim=true) 
{ 
	$both_ends = $ltrim && $rtrim; 

	$char_class_inner = preg_replace( 
		array( '/[\^\-\]\\\]/S', '/\\\{4}/S' ), 
		array( '\\\\\\0', '\\' ), 
		$charlist 
	); 

	$work_horse = '[' . $char_class_inner . ']+'; 
	$ltrim && $left_pattern = '^' . $work_horse; 
	$rtrim && $right_pattern = $work_horse . '$'; 

	if($both_ends) 
	{ 
		$pattern_middle = $left_pattern . '|' . $right_pattern; 
	} 
	elseif($ltrim) 
	{ 
		$pattern_middle = $left_pattern; 
	} 
	else 
	{ 
		$pattern_middle = $right_pattern; 
	} 

	return preg_replace("/$pattern_middle/usSD", '', $string) ; 
} 

/*
function mb_trim($str, $chars='\s ') { 
  $str = preg_replace("/^[$chars]+/u", '', $str); 
  $str = preg_replace("/[$chars]+$/u", '', $str); 
  return $str; 
}
*/

// A multibyte one-to-one alternative for the str_split function
function mb_str_split($string, $split_length = 1){
            
        mb_internal_encoding('UTF-8'); 
        mb_regex_encoding('UTF-8');  
        
        $split_length = ($split_length <= 0) ? 1 : $split_length;
        
        $mb_strlen = mb_strlen($string, 'utf-8');
        
        $array = array();
                
        for($i = 0; $i < $mb_strlen; $i + $split_length){
        
            $array[] = mb_substr($string, $i, $split_length); 
        }

        return $array;
    
    }

function mb_substr_replace($output, $replace, $posOpen, $posClose) 
{ 
	return 	mb_substr($output, 0, $posOpen)
			.$replace
			.mb_substr($output, $posClose+1); 
 } 

 
 
 
if(!function_exists('mb_str_replace')) {

    function mb_str_replace($search, $replace, $subject) {

        if(is_array($subject)) {
            $ret = array();
            foreach($subject as $key => $val) {
                $ret[$key] = mb_str_replace($search, $replace, $val);
            }
            return $ret;
        }

        foreach((array) $search as $key => $s) {
            if($s == '') {
                continue;
            }
            $r = !is_array($replace) ? $replace : (array_key_exists($key, $replace) ? $replace[$key] : '');
            $pos = mb_strpos($subject, $s);
            while($pos !== false) {
                $subject = mb_substr($subject, 0, $pos) . $r . mb_substr($subject, $pos + mb_strlen($s));
                $pos = mb_strpos($subject, $s, $pos + mb_strlen($r));
            }
        }

        return $subject;

    }

}

function mb_str_pad ($input, $pad_length, $pad_string, $pad_style, $encoding="UTF-8") 
{ 
	return str_pad(
			$input, 
			strlen($input)-mb_strlen($input,$encoding)+$pad_length, 
			$pad_string, 
			$pad_style
			); 
} 

 
